package com.igeek.collateral.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.igeek.collateral.dto.ChildDto;
import com.igeek.collateral.dto.FatherDto;
import com.igeek.collateral.entity.Menu;
import com.igeek.collateral.entity.Meta;
import com.igeek.collateral.exception.HealthException;
import com.igeek.collateral.mapper.MenuMapper;
import com.igeek.collateral.mapper.MetaMapper;
import com.igeek.collateral.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.collateral.vo.MenuTableVo;
import com.igeek.collateral.vo.MenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-17
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MetaMapper metaMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<MenuVo> findAll() {
        List<MenuVo> list = new ArrayList<>();
//查询一级目录
        List<Map<String, Object>> first1 = menuMapper.selectFirst();
        List<Menu> menus = new ArrayList<>();
        List<Meta> metas = new ArrayList<>();
        first1.forEach(map -> {
            Meta meta = new Meta();
            Menu menu = new Menu();
            menu.setPath((String) map.get("path"));
            menu.setName((String) map.get("name"));
            meta.setTitle((String) map.get("title"));
            meta.setIcon((String) map.get("icon"));
            metas.add(meta);
            menus.add(menu);
        });
        menus.forEach(menu -> {
            String path = menu.getPath();
            List<Map<String, Object>> second = menuMapper.selectMenuList(path);
            MenuVo first = new MenuVo();
            List<MenuVo> children = new ArrayList<>();
            if (second.size() == 0) {
                first.setName(menu.getName());
                first.setPath(menu.getPath());
                first.setMeta(metas.get(0));

            } else {
                second.forEach(map -> {
                    first.setName((String) map.get("name1"));
                    first.setPath((String) map.get("path1"));
                    if (first.getMeta() == null) {
                        first.setMeta(metas.get(0));
                        metas.remove(metas.get(0));
                    }
                    MenuVo second1 = new MenuVo();
                    second1.setPath((String) map.get("path2"));
                    second1.setComponent((String) map.get("component"));
                    second1.setName((String) map.get("name2"));
                    Meta meta1 = new Meta();
                    meta1.setTitle((String) map.get("title"));
                    meta1.setIcon((String) map.get("icon"));
                    second1.setMeta(meta1);
                    children.add(second1);
                });
            }
            first.setChildren(children);
            list.add(first);
        });
        return list;
    }

    @Override
    public List<MenuTableVo> findAllMenu() {
        List<MenuTableVo> list = new ArrayList<>();
//查询一级目录
        List<Map<String, Object>> first1 = menuMapper.selectFirst();
        List<Menu> menus = new ArrayList<>();
        List<Meta> metas = new ArrayList<>();
        first1.forEach(map -> {
            Meta meta = new Meta();
            Menu menu = new Menu();
            menu.setPath((String) map.get("path"));
            meta.setTitle((String) map.get("title"));
            meta.setIcon((String) map.get("icon"));
            menu.setName((String) map.get("name"));
            menu.setLevel((Integer) map.get("level"));
            menu.setPriority((Integer) map.get("priority"));
            menu.setMetaId((Integer) map.get("meta_id"));
            menu.setDescription((String) map.get("description"));

            meta.setId((Integer) map.get("id"));
            metas.add(meta);
            menus.add(menu);
        });
        menus.forEach(menu -> {
            String path = menu.getPath();
            List<Map<String, Object>> maplist = menuMapper.selectMenuList1(path);
            MenuTableVo first = new MenuTableVo();
            List<MenuTableVo> children = new ArrayList<>();
            if (maplist.size() == 0) {
                first.setName(menu.getName());
                first.setPath(menu.getPath());
                first.setLevel(menu.getLevel());
                first.setPriority(menu.getPriority());
                first.setComponent(menu.getComponent());
                first.setDescription(menu.getDescription());
                first.setParentMenuId(menu.getParentMenuId());
                first.setMetaId(menu.getMetaId());
                first.setMeta(metas.get(0));

            }
            maplist.forEach(map -> {

                first.setId((Integer) map.get("id1"));
                first.setMetaId((Integer) map.get("meta_id1"));
                first.setPath((String) map.get("path1"));
                first.setName((String) map.get("name1"));
                first.setLevel((Integer) map.get("level1"));
                first.setPriority((Integer) map.get("priority1"));


                first.setDescription((String) map.get("description1"));

                if (first.getMeta() == null) {
                    first.setMeta(metas.get(0));
                    metas.remove(metas.get(0));
                }
                MenuTableVo second1 = new MenuTableVo();

                second1.setId((Integer) map.get("id2"));
                second1.setMetaId((Integer) map.get("meta_id"));
                second1.setPath((String) map.get("path2"));
                second1.setName((String) map.get("name2"));
                second1.setLevel((Integer) map.get("level2"));
                second1.setPriority((Integer) map.get("priority2"));
                second1.setParentMenuId((Integer) map.get("parentMenuId2"));
                second1.setComponent((String) map.get("component"));
                second1.setDescription((String) map.get("description2"));

                Meta meta1 = new Meta();
                meta1.setTitle((String) map.get("title"));
                meta1.setIcon((String) map.get("icon"));
                meta1.setId((Integer) map.get("meta_id"));
                second1.setMeta(meta1);
                children.add(second1);
            });
            first.setChildren(children);
            list.add(first);
        });
        return list;

    }

    @Override
    public boolean addF(FatherDto fatherDto) {
        Menu menu = new Menu();
        Meta meta = new Meta();
        meta.setTitle(fatherDto.getTitle());
        meta.setIcon(fatherDto.getIcon());
        int i = metaMapper.insert(meta);
        log.info("meta插入成功"+meta);

        if (i > 0) {
//            Integer metaId = meta.getId();
//            menu.setMetaId(metaId);
            menu.setPriority(fatherDto.getPriority());
            menu.setName(fatherDto.getName());
            menu.setPath(fatherDto.getPath());
            menu.setDescription(fatherDto.getDescription());
            menu.setComponent(fatherDto.getComponent());
            int j=menuMapper.insert(menu);

            if (j > 0) {
                boolean flag = redisTemplate.delete("menu::");
                return Boolean.TRUE.equals(flag);
            }
        }
        return i > 0 ? true : false;
    }

    @Override
    public boolean addS(ChildDto childDto, Integer fatherMenuId) {
        Menu menu = new Menu();
        Meta meta = new Meta();
        meta.setTitle(childDto.getTitle());
        meta.setIcon(childDto.getIcon());
        int i = metaMapper.insert(meta);

        if (i > 0) {
            Integer metaId = meta.getId();
            menu.setMetaId(metaId);
            menu.setName(childDto.getName());
            menu.setPath(childDto.getPath());
            menu.setPriority(childDto.getPriority());
            menu.setLevel(childDto.getLevel());
            menu.setComponent(childDto.getComponent());
            menu.setDescription(childDto.getDescription());
            menu.setParentMenuId(fatherMenuId);
            int i1 = menuMapper.insert(menu);
            if (i1 > 0) {
                Boolean delete = redisTemplate.delete("");
                return Boolean.TRUE.equals(delete);
            }


        }
        return false;
    }

    @Override
    public List<Menu> selectFather() {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Menu::getLevel , 1);
        List<Menu> menus = this.baseMapper.selectList(lambdaQueryWrapper);
        return menus;
    }

    @Override
    public boolean upadateSonMenu(ChildDto childDto, Integer fatherMenuId) {


        return false;
    }

    @Override
    public boolean upadateFatherMenu(FatherDto fatherDto) {
        Menu menu = menuMapper.selectById(fatherDto.getId());
        Meta meta = metaMapper.selectById(menu.getMetaId());
        menu.setPriority(fatherDto.getPriority());
        menu.setName(fatherDto.getName());
        menu.setComponent(fatherDto.getComponent());
        menu.setPath(fatherDto.getPath());
        menu.setDescription(fatherDto.getDescription());
        meta.setTitle(fatherDto.getTitle());
        meta.setIcon(fatherDto.getIcon());
        metaMapper.updateById(meta);
       int i= menuMapper.updateById(menu);
       if (i>0){
           Boolean delete = redisTemplate.delete("");
           return Boolean.TRUE.equals(delete);
       }
        return false;
    }

    @Override
    public boolean deleteById(Integer id) {
    //查询是否存在子菜单
        if (menuMapper.selectCount(new QueryWrapper<Menu>().eq("parentMenuId",id))==0){
            int i = menuMapper.deleteById(id);
            if (i>0){
                Boolean delete = redisTemplate.delete("menu::com.igeek.factory.service.impl.MenuServiceImplgetMenuList");
                return Boolean.TRUE.equals(delete);
            }
        }
        else{
            throw new HealthException("该菜单下存在子菜单，无法删除");
        }
        return true;
    }
}



